package com.activiti.z_six.controller;

import com.activiti.z_six.entity.UserInfo;
import com.activiti.z_six.entity.orgmanagement.UserEntity;
import com.activiti.z_six.service.OrgManagementService;
import com.activiti.z_six.service.impl.UserInfoServiceImpl;
import com.activiti.z_six.util.AjaxResponse;
import com.activiti.z_six.util.SystemConfig;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

@RestController
public class UserInfoController {
    @Autowired
    UserInfoServiceImpl userInfoService;
    @Autowired
    OrgManagementService orgManagementService;

    @PostMapping(value = "/login/loginAs")
    public AjaxResponse loginAs(@RequestBody JSONObject param){
        String username=param.getString("username");
        String password=param.getString("password");
        try{
            HashMap<String,Object> hashMap=userInfoService.loginAs(username,password);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), hashMap);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), "");
        }
    }
    @PostMapping(value = "/login/refToken")
    public AjaxResponse refToken(@RequestBody JSONObject param){
        String refresh_token=param.getString("refresh_token");
        try{
            HashMap<String,Object> hashMap=userInfoService.refToken(refresh_token);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), hashMap);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), "");
        }
    }
    /**
    根据登录名获取用户信息
     */
    @GetMapping(value="/login/getUserInfo")
    public AjaxResponse getUserInfo(@RequestParam("username") String username, @RequestParam("password") String password){
        UserInfo userInfo= userInfoService.getUserInfo(username);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        JSONObject obj=new JSONObject();
        if(userInfo!=null) {
            if(encoder.matches(password,userInfo.getPassword())) {
                obj.put("username",userInfo.getUsername());
                obj.put("name",userInfo.getName());
                return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                        SystemConfig.ResponseCode.SUCCESS.getDesc(), obj);
            }
            else
                return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), "");
        }
        else
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), "");
    }
    @GetMapping(value="/login/getInfo")
    public AjaxResponse getInfo(@RequestParam("username") String username){
        UserInfo userInfo= userInfoService.getUserInfo(username);
        HashMap<String,Object> json=new HashMap<>();
        JSONObject user=new JSONObject();
        if(userInfo!=null) {
            user.put("username",userInfo.getUsername());
            user.put("name",userInfo.getName());
        }

        json.put("user",user);

        return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                SystemConfig.ResponseCode.SUCCESS.getDesc(), json);
    }
}
